<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\UploadForm;
use yii\web\UploadedFile;

/**
 * Site controller
 */
class SiteController extends Controller
{
    /**p
     * @inheritdoc
     */

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * @return  index page
     */
    public function actionIndex()
    {
        // $session = Yii::$app->session;
        // $name_user = "18916294855";
        // $name_nick = "doc_8872";
        // $session->set('username',$name_user);
        // $session->set('nickname',$name_nick);
        // $username = $session->get('username');
        // $nickname = $session->get('nickname');
        // if(!empty($username)){
        //     $this->nickname = $nickname;
        //     $this->regLogin = "退出";
        // }else{
        //     $this->regLogin = "登录/注册";
        // }
        return $this->render('index/main');
    }

    /**
     * @return login page
     */
    public function actionLogin()
    {
        $model = new UploadForm();
        if(Yii::$app->request->isAjax)
        {
            $data = Yii::$app->request->post();
            $username= explode(":", $data['username']);
            $password= explode(":", $data['password']);
            $username = $username[0];
            $password= $password[0];
            
            //验证用户的密码
            $db = Yii::$app->db;
            $sel_sql = "SELECT * FROM con_user where username=$username";
            $getinfo =  $db->createCommand($sel_sql)->queryOne();
            $re_data=[
                'status' => '100',
            ];
            if($getinfo['password'] == $password)
            {
                $re_data['status'] = '200';
                $re_data['nickname'] = $getinfo['nickname'];
                $re_data['userId'] = $getinfo['id'];

                // session设置
                $session = Yii::$app->session;
                $session->set('username' , $username);
                $session->set('nickname',$re_data['nickname']);
                $session->set('userId',$getinfo['id']);
            }

            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $re_data;
         }
        return $this->render('login/main', ['model' => $model]);
    }

    /*  退出登录状态 */
    public function actionLogout()
    {
        $session = Yii::$app->session;
        $session->set('username','');
        $session->set('nickname','');
        $session->set('userId','');
        return $this->render('index/main');
    }

    /**
     * @return register page
     */
    public function actionRegister()
    {
        $model = new UploadForm();

        if (Yii::$app->request->isPost) {
            // $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
            $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model->upload()) {
                // 文件上传成功
                 // $this->render('index/main', ['model' => $model]);
                return $this->render('index/main');
                // return;
            }
        }
        return $this->render('register/main', ['model' => $model]);

        // return $this->render('register/main');
    }

    /*
    * uType 1表示医生 2表示普通用户
    */
    public function actionReginfo()
    {
        if (Yii::$app->request->isAjax) {
            $data = Yii::$app->request->post();

            $uType= explode(":", $data['uType']);
            $uTel= explode(":", $data['uTel']);
            $uVerycode= explode(":", $data['uVerycode']);
            $uPassword = explode(":", $data['uPassword']);
            $uTel = $uTel[0];
            $uVerycode= $uVerycode[0];
            $uPassword = $uPassword[0];
            $uType = $uType[0];
            
            //插入数据库 
            $db = Yii::$app->db;   
            $time = strtotime(date('Y-m-d',time()).' 00:00:00');
            $nickname = "doc_".$uVerycode;
            $insert_data = [
                'username' => $uTel,
                'password' => $uPassword,
                'user_type' => $uType,
                'nickname' => $nickname,
                'ctime' => $time
            ];
            $re_data=[
                'status' => '100',
            ];
            $rep = $db->createCommand()->insert('con_user',$insert_data)->execute();
            if($rep){
                $re_data['status'] = '200';

                //创建图片文件夹
                $path = "testupload/";
                $path=$path.$uTel;
                mkdir($path,0777);
                chmod($path,0777);

                //获取用户的id号
                $sel_sql = 'SELECT id FROM con_user where username=$uTel';
                $idinfo = $db->createCommand($sel_sql)->queryOne();
                // 设置session
                $session = Yii::$app->session;
                $session->set('username' , $uTel);
                $session->set('nickname',$nickname);
                $session->set('userId',$idinfo['id']);
            }
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $re_data;
         }
    }

    /*
        * 手机号码注册验证 
    */
    public function actionRegphone()
    {
        if (Yii::$app->request->isAjax) 
        {
            $data = Yii::$app->request->post();
            $db = Yii::$app->db;

            $username= explode(":", $data['username']);
            $username= $username[0];
            
            $re_data=[
                'status' => '100',
            ];
            $sql = "SELECT * FROM con_user where username=$username";
            $getinfo =  $db->createCommand($sql)->queryOne();
            if(!empty($getinfo)){
                $re_data['status'] = '100';
            }else{
                $re_data['status'] = '200';
            }
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $re_data;
         }

    }
    /*
        *对主题进行请求 弹出主题列表页面
    */
    public function actionTheme()
    {
        $tmp = Yii::$app->request->queryString;
        $type = substr($tmp,-1);   
        $data = [
            'status' => 100,
        ];
        if(!empty($type))
        {
            //数据库查询
            $db = Yii::$app->db;
            $sel_sql = "SELECT * FROM con_article where themeId=$type";
            $getinfo =  $db->createCommand($sel_sql)->queryAll();  

            //获取评论数据
            if(!empty($getinfo))
            {
                $data['status'] = 200;
                $length = count($getinfo);
                for ($i=0; $i <$length ; $i++) {             
                    $article_id = $getinfo[$i]['id'];
                    $sel_sql = "SELECT count(*) FROM con_tips where articleId=$article_id";
                    $count = Yii::$app->db->createCommand($sel_sql)->queryScalar();
                    if($count){
                        $getinfo[$i]['answer'] = $count;
                    }else{
                        $getinfo[$i]['answer'] = 0;
                    }
                    //时间 作者
                    $tm=date("Y-m-d H:i:s",$getinfo[$i]['ctime']);
                    $getinfo[$i]['time'] = $tm;

                    //作者
                    $user_id = $getinfo[$i]['userId'];
                    $sel_sql = "SELECT nickname FROM con_user where id=$user_id";
                    $userinfo = $db->createCommand($sel_sql)->queryOne();
                    $getinfo[$i]['author'] = $userinfo['nickname'];
                }
                $data['article'] = $getinfo;
            }
        }    
        return $this->render('theme/main',['data' => $data]);
    }

    /*
      * article 获取文章的回复列表和具体信息
    */
    public function actionDetarticle()
    {
        $tmp = Yii::$app->request->queryString;
        $hello = explode('=',$tmp); 

        $article_id = $hello[count($hello)-1]; 
        $data = [
            'status' => 100,
        ];
      
        if(!empty($article_id))
        {
            //数据库查询
            $db = Yii::$app->db;
            //step1: 文章信息
            $sel_sql = "SELECT * FROM con_article where id=$article_id";
            $artinfo = $db->createCommand($sel_sql)->queryOne();

            //获取所有的评论信息
            if(!empty($artinfo))
            {
                 //时间 作者
                $data['status'] = 200;
                $tm=date("Y-m-d H:i:s",$artinfo['ctime']);
                $artinfo['time'] = $tm;
                //作者
                $user_id = $artinfo['userId'];
                $sel_sql = "SELECT nickname FROM con_user where id=$user_id";
                $userinfo = $db->createCommand($sel_sql)->queryOne();
                $artinfo['author'] = $userinfo['nickname'];

                //获取所有的评论信息
                $sel_sql = "SELECT * FROM con_tips where articleId=$article_id";
                $tipinfo = Yii::$app->db->createCommand($sel_sql)->queryAll();
                $length = count($tipinfo);
                $artinfo['answer'] = $length;  //回答数量
                $data['artinfo'] = $artinfo;

                for ($i=0; $i <$length ; $i++) {             
                    //作者
                    $user_id = $tipinfo[$i]['from_id'];
                    $sel_sql = "SELECT nickname,user_type FROM con_user where id=$user_id";
                    $userinfo = $db->createCommand($sel_sql)->queryOne();
                    $tipinfo[$i]['nickname'] = $userinfo['nickname'];
                    $tipinfo[$i]['user_type'] = $userinfo['user_type'];

                    $tm=date("Y-m-d H:i:s",$tipinfo[$i]['ctime']);
                    $tipinfo[$i]['time'] = $tm;
                }
                $data['tipinfo'] = $tipinfo;
            }
        }    
        return $this->render('detarticle/main',['data' => $data]);
    }

    /*
     *实现文章回复评论提交
    */
    public function actionComment()
    {
        if (Yii::$app->request->isAjax) 
        {
            $data = Yii::$app->request->post();
            $db = Yii::$app->db;
            $content= explode(":", $data['content']);
            $articleId = explode(":", $data['articleId']);
            $content= $content[0];
            $articleId= $articleId[0];
            
            $session = Yii::$app->session;
            $from_id = $session->get('userId');
            $to_id = '1';
            $tm = time();
            $insert_data=[
                'from_id' => $from_id,
                'to_id' => $to_id,
                'content' => $content,
                'ctime' => $tm,
                'articleId' => $articleId,
            ];

            $re_data=[
                'status' => '100',
            ];
            $rep = $db->createCommand()->insert('con_tips',$insert_data)->execute();
            if($rep){
                $re_data['status'] = '200';
            }
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $re_data;
         }
        // return $this->render('testdb/main',$data);
    }

    /*
        * 实现问题跳转
    */
    public function actionQuestion()
    {
        return $this->render('question/main');
    }
    /*
      * 实现发布问题功能 
    */
    public function actionArticletip()
    {
        if (Yii::$app->request->isAjax) 
        {
            $data = Yii::$app->request->post();
            $db = Yii::$app->db;
            $content= explode(":", $data['content']);
            $title = explode(":", $data['title']);
            $themeId = explode(":", $data['themeId']);
            $content= $content[0];
            $title= $title[0];
            $themeId= $themeId[0];
            
            $session = Yii::$app->session;
            $userId = $session->get('userId');
            $tm = time();
            $insert_data=[
                'userId' => $userId,
                'title' => $title,
                'content' => $content,
                'ctime' => $tm,
                'themeId' => $themeId,
            ];

            $re_data=[
                'status' => '100',
            ];
            $rep = $db->createCommand()->insert('con_article',$insert_data)->execute();
            if($rep){
                $re_data['status'] = '200';
            }
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $re_data;
         }
    }
    /*
      * 跳转到专家列表页面
    */
    public function actionExpert()
    {
        //获取专家列表
        $db= Yii::$app->db;
        $sel_sql = "SELECT * FROM con_expert";
        $getinfo = Yii::$app->db->createCommand($sel_sql)->queryAll();

        return $this->render('expert/main',['data' => $getinfo]);
    }

    /*
        test database
    */
    public function actionTestdb()
    {
        // $db = Yii::$app->db;
        // // $userinfo =  $db->createCommand('SELECT * FROM con_user')
        // //     ->queryAll();
        // $username = "18916294857";
        // $sql = "SELECT * FROM con_user where username=$username";
        // $getinfo =  $db->createCommand($sql)->queryOne();
        // $re_data=[
        //         'testData' => 'helpdata',
        // ];
        // // $re_data['nickname']=$getinfo['nickname'];
        // // $re_data['password'] = $getinfo['password'];
        // // if($getinfo['password'] =="123")
        // //     $getinfo['status'] = '200';
        // //     $data = [
        // //             'testData' => "fuck",
        // //             're_data' => $getinfo,
        // //         ];
        // // }else{
        // if(!empty($getinfo)){
        //     $getinfo['status'] = '200';
        //     $data = [
        //             'testData' => "fuck",
        //             're_data' => $getinfo,
        //         ];
        // }
        // else{
        //     $getinfo['status'] = '100';
        //     $data = [
        //         'testData' => "fuck",
        //         're_data' => $getinfo,
        //     ];
        // }
        // }
        // $time = strtotime(date('Y-m-d',time()).' 00:00:00');
        // $insert_data = [
        //     'username' => '18916293322',
        //     'password' => '123456',
        //     'user_type' => '2',
        //     'nickname' => 'doc_3321',
        //     'ctime' => $time
        // ];

        // $db->createCommand()->insert('con_user',$insert_data)->execute();
        // $re_data=[
        //     'status' => '100',
        // ];
        // $re_data['nickname']='nickname';
        // $re_data['password'] = 'password';
        // $re_data['session'] = 'fuck';
        // $path = "testupload/";
        // $username = "18916294857";
        // $path=$path.$username;
        // mkdir($path,0777);
        // chmod($path,0777);

        // $session = Yii::$app->session;
        // $re_data['session'] = $session->get('username');

        // $data = ['testData' => 'helpdata',
        //         're_data' => $re_data,
        //     ];
        $tmp = Yii::$app->request->queryString;
        $getinfo['status'] = '200';

        $help = substr($tmp,-1);
        $getinfo['fuck'] = $help;
        $data = [
                'testData' => "fuck",
                 're_data' => $getinfo,
           ];

        return $this->render('testdb/main',$data);
    }
   


    //未完善的功能
    public function actionService()
    {
       
        return $this->render('service/main');
    }
    public function actionTelephone()
    {
       
        return $this->render('service/main');
    }
        public function actionGetexpert()
    {
       
        return $this->render('service/main');
    }
}























